package cn.itcast.bos.service.action.teke_delivary.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivary.Promotion;
import cn.itcast.bos.service.action.teke_delivary.PromotionService;
import cn.itcast.bos.web.Dao.teke_delivary.PromotionRepository;

@Service
@Transactional
public class PromotionServiceimpl implements PromotionService {

	// 注入DAO
	@Autowired
	private PromotionRepository promotionRepository;

	// 宣传活动信息的添加
	@Override
	public void save(Promotion promotion) {

		promotionRepository.save(promotion);
	}

	/// 后台分页查询
	@Override
	public Page<Promotion> findPageData(Pageable pageable) {

		return promotionRepository.findAll(pageable);
	}

	// 前台页面的分页查询
	@Override
	public PageBean<Promotion> findPageData(int page, int rows) {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Promotion> pageData = promotionRepository.findAll(pageable);

		// 封装到Page对象
		PageBean<Promotion> pageBean = new PageBean<Promotion>();
		pageBean.setTotalCount(pageData.getTotalElements());
		pageBean.setPageData(pageData.getContent());

		// 返回pageBean
		return pageBean;
	}

	// 静态页面的加载 查询 ID 是否存在
	@Override
	public Promotion findById(Integer id) {
		return promotionRepository.findOne(id);
	}

	// 修改宣传活动状态操作
	@Override
	public void updateStatus(Date date) {
		promotionRepository.updateStatus(date);
	}

}
